#include <stdio.h>  
#include <stdlib.h>  
#include <unistd.h>  
#include <sys/types.h>  
#include <sys/socket.h>  
#include <netinet/in.h>  
#include <string.h>  
#include <sys/select.h>  
#include <arpa/inet.h>
#include <iostream>

#define MAX_CLIENTS 10
#define BUFFER_SIZE 1024
#define PORT 8080

void print_fd_set(fd_set &readfds) {
    for (int i = 0; i < (__FD_SETSIZE / __NFDBITS); i++) {
        std::cout << readfds.fds_bits[i] << "\t";
    }
    std::cout << std::endl;
}

int main() {
    int server_fd, client_fd;
    struct sockaddr_in server_addr, client_addr;
    socklen_t client_len = sizeof(client_addr);
    fd_set readfds;
    struct timeval tv;
    int max_fd = 0;
    int nready;
    char buffer[BUFFER_SIZE];
  
    // 创建服务器套接字
    if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
        perror("socket");
        exit(EXIT_FAILURE);
    }
  
    // 设置服务器地址
    memset(&server_addr, 0, sizeof(server_addr));
    server_addr.sin_family = AF_INET;
    server_addr.sin_addr.s_addr = INADDR_ANY;
    server_addr.sin_port = htons(PORT);
  
    // 绑定服务器套接字到地址
    if (bind(server_fd, (struct sockaddr *)&server_addr, sizeof(server_addr)) == -1) {
        perror("bind");
        exit(EXIT_FAILURE);
    }
  
    // 监听连接
    if (listen(server_fd, MAX_CLIENTS) == -1) {
        perror("listen");
        exit(EXIT_FAILURE);
    }
  
    // 设置文件描述符集合并添加服务器套接字

    std::cout << "sizeof(long int)=" << sizeof(long int) << std::endl;
    std::cout << "__FD_SETSIZE / __NFDBITS :" << (__FD_SETSIZE / __NFDBITS) << std::endl;
    FD_ZERO(&readfds);
    std::cout << "FD_ZERO readfds:" << std::endl;
    print_fd_set(readfds);
    
    FD_SET(server_fd, &readfds);
    std::cout << "server_fd:" << server_fd << std::endl;
    std::cout << "FD_SET readfds:" << std::endl;
    print_fd_set(readfds);

    max_fd = server_fd;
  
    while (1) {
        // 复制文件描述符集合，因为select()会修改它
        fd_set rset = readfds;
        
        // 设置超时时间
        tv.tv_sec = 5;
        tv.tv_usec = 0;

        // 等待文件描述符就绪
        nready = select(max_fd + 1, &rset, NULL, NULL, &tv);
        std::cout << "select rset:" << std::endl;
        print_fd_set(rset);
  
        if (nready == -1) {
            perror("select");
            exit(EXIT_FAILURE);
        } else if (nready == 0) {
            printf("No data within five seconds.\n");
            continue;
        }
  
        // 检查服务器套接字是否有新的连接请求
        if (FD_ISSET(server_fd, &rset)) {
            client_fd = accept(server_fd, (struct sockaddr *)&client_addr, &client_len);
            if (client_fd == -1) {
                perror("accept");
                continue;
            }
  
            printf("Accepted new connection from %s:%d\n",
                inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port));
  
            // 将新客户端的文件描述符添加到集合中，并更新最大文件描述符值
            FD_SET(client_fd, &readfds);
            if (client_fd > max_fd) {
                max_fd = client_fd;
            }
        }
  
        // 处理其他客户端套接字...
        // 注意：在这个例子中，我们只接受新的连接请求，而不处理已连接客户端的数据。
  
        // 清除集合以便下一次迭代
        std::cout << "readfds:" << std::endl;
        print_fd_set(readfds);

        FD_ZERO(&readfds);
        FD_SET(server_fd, &readfds);
        max_fd = server_fd;
    }
  
    // 关闭套接字
    close(server_fd);
  
    return 0;  
}